<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use think\Exception;//事务处理

class Department extends Model
{
    //部门的列表页
    public function getdepartment($res)
    {
        $where['a.lang'] = $res['lang'];//语言标签切换
        $where['b.country_id'] = $res['country_id'];//国家标签切换
        $join = [
            ['nt_department b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        if (isset($res['dname']) && $res['dname']) {
            $where['a.content'] = array('like', '%' . trim($res['dname']) . '%');//部门名称搜索
            $resdata = getmultidata($table, $join, $where);
            $data = $resdata['data'];//数据
            $count = $resdata['count'];//总数
            foreach ($data as $datakey => $dataval) {
                $data[$datakey]['isexist'] = 2;
                $data[$datakey]['level'] = 0;
                //部门下的员工的数量
                $data[$datakey]['num'] = Db::name('admin')->where(['department_id' => $dataval['id']])->count();
            }
        } else {
            $resdata = getmultidata($table, $join, $where);
            $data = getTree($resdata['data']);
            $count = $resdata['count'];
            foreach ($data as $datakey => $dataval) {
                $infoframe = $this->where('pid', $dataval['id'])->find();
                if ($infoframe) {
                    $data[$datakey]['isexist'] = 1;
                } else {
                    $data[$datakey]['isexist'] = 2;
                }
                //部门下的员工的数量
                $data[$datakey]['num'] = Db::name('admin')->where(['department_id' => $dataval['id']])->count();
            }
        }
        return array('count' => $count, 'data' => $data);
    }

    //处理部门的添加，子添加 编辑
    public function addeditdepartment($data)
    {
        if ($data['act'] == 'departmentadd' || $data['act'] == 'departmentadds') {
            //添加子添加
            $ckdata = notEmpty(['frame' => lang('department_name')]);
        } else {
            //编辑
            $ckdata = notEmpty(['lang_zh' => lang('Chinese_department_name'), 'lang_en' => lang('English_department_name'), 'lang_pt' => lang('Portugues_department_name')]);
            $dname_zh = trim($data['lang_zh']);//中文
            $dname_en = trim($data['lang_en']);//英文
            $dname_pt = trim($data['lang_pt']);//葡萄牙文
        }
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        $lang = $data['lang'];
        unset($data['lang']);
        if ($data['act'] == 'departmentadd' || $data['act'] == 'departmentadds') {
            //判断部门名称是否存在
            $depalike = 'department';
            $langwhere['mkey'] = array('like', $depalike . '%');
            $framename = $langwhere['content'] = trim($data['frame']);
            $langzh = getables('multi_lang', $langwhere);
            if ($langzh) {
                return array('msg' => $framename . lang('department_exists'), 'status' => 3);
            }
            //添加 和子添加
            Db::startTrans();
            $data['create_at'] = $data['update_at'] = time();
            $departmentid = $this->strict(false)->insertGetId($data);//部门表
            //更新数据
            $departmentidmkey = 'department_' . $departmentid;
            $this->where('id', $departmentid)->setField('mkey', $departmentidmkey);
            $langdata = getables('language');
            $multidata = array();
            foreach ($langdata as $langdatakey => $langdataval) {
                $multidata[$langdatakey]['content'] = $framename;
                $multidata[$langdatakey]['mkey'] = $departmentidmkey;
                $multidata[$langdatakey]['lang'] = $langdataval['code'];
            }
            $langid = Db::name('multi_lang')->insertAll($multidata);//语言表
            if ($departmentid && $langid) {
                Db::commit();
                return array('msg' => $framename . lang('added_successfully'), 'status' => 1);
            } else {
                return array('msg' => $framename . lang('add_failed'), 'status' => 0);
            }
        } elseif ($data['act'] == 'departmentedit') {
            switch ($lang) {
                case 'zh':
                    $langedit = $dname_zh;
                    break;
                case 'en':
                    $langedit = $dname_en;
                    break;
                case 'pt':
                    $langedit = $dname_pt;
                    break;
                default:
                    $langedit = $dname_zh;
                    break;
            }
            //判断编辑是否有误
            if ($data['id'] == $data['pid']) {
                return array('msg' => $langedit . lang('edited_wrong'), 'status' => 0);
            }
            $inpid = getinfo($data['pid'], 'department')['pid'];//要编辑部门的pid
            if ($inpid == $data['id']) {
                return array('msg' => $langedit . lang('edited_wrong'), 'status' => 0);
            }
            Db::startTrans();
            $datadepart['update_at'] = time();
            $datadepart['pid'] = $data['pid'];
            $datadepart['country_id'] = $data['country_id'];
            $resupid = $this->where('id', $data['id'])->update($datadepart);//更新部门表
            //更新语言表
            $departmkey = 'department_' . $data['id'];
            $langupidzh = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'zh'])->setField('content', $dname_zh);//更新中文部门名称
            $langupiden = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'en'])->setField('content', $dname_en);//更新英文部门名称
            $langupidpt = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'pt'])->setField('content', $dname_pt);//更新葡文部门名称
            if ($resupid !== false && $langupidzh !== false && $langupiden !== false && $langupidpt !== false) {
                Db::commit();
                return array('msg' => $langedit . lang('edited_successfully'), 'status' => 1);
            } else {
                return array('msg' => $langedit . lang('edit_failed'), 'status' => 0);
            }
        }
    }

    //处理删除的部门的信息
    public function deldepartment($data)
    {
        $lang = $data['lang'];
        //查询具体语言下的部门
        $framewhere['mkey'] = 'department_' . $data['id'];
        $langname = getlangname('multi_lang', $framewhere, $lang);

        //在删除时，若有子部门的不允许删除
        $dpres = $this->where('pid', $data['id'])->find();
        if ($dpres) {
            return array('msg' => $langname . lang("contains_sublevel_doesn't_allow_deletion"), 'status' => 0);
        }
        //部门中含有用户不允许删除
        $info = Db::name('admin')->where('department_id', $data['id'])->find();
        if ($info) {
            return array('msg' => $langname . lang("contains_employees_not_allowed_to_delete"), 'status' => 0);
        }
        Db::startTrans();
        $retdelid = $this->where('id', $data['id'])->delete();//删除对应的部门表
        $langdel = Db::name('multi_lang')->where('mkey', 'department_' . $data['id'])->delete();
        if ($retdelid && $langdel) {
            Db::commit();
            return array('msg' => $langname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $langname . lang("failed_to_delete"), 'status' => 0);
        }
    }
}


